import java.util.Arrays;
import java.util.Collection;
import java.util.ArrayList;
import ExceptionPackage.*;
public class MyArrayList {
    public int UseSize;
    public int[] array;
    public MyArrayList(int initialCapacity){
        if(initialCapacity<=0){
            throw new RuntimeException("自定义容量出错");
        }
        this.array = new int[initialCapacity];
    }
    public MyArrayList(){
        this.array = new int[0];
    }
    public boolean isEmpty(){
        return this.UseSize==0;
    }
    public boolean PosOutofBrond(int pos){
        if(pos<0||pos>=this.UseSize){
            return true;
        }else{
            return false;
        }
    }

    public void CheckCapacity(){
        if(this.UseSize==0){
            this.array = Arrays.copyOf(array,5);
        }else if(this.UseSize==this.array.length){
            this.array = Arrays.copyOf(array,2*this.UseSize);
        }

    }



    // 打印顺序表
        public void display() {
            if(isEmpty()){
                return;
            }else{
                for(int i = 0;i<UseSize;i++){
                    System.out.print(array[i]+" ");
                }
            }
        }
        // 新增元素,默认在数组最后新增
        public void add(int data) {
        //先判断容量
            CheckCapacity();
            this.array[UseSize] = data;
            this.UseSize++;
    }
        // 在 pos 位置新增元素
        public void add(int pos, int data) {
            //判断pos下标是否合法
        if(PosOutofBrond(pos)){
            throw new PosOutofBrond("pos位置不合法");
        }else{
            this.array[pos] = data;
        }

    }
        // 判定是否包含某个元素
        public boolean contains(int toFind) {
        if(isEmpty()){
            throw new EmptyArrayListException("该顺序表是空的");
        }else{
            for(int i = 0;i<this.UseSize;i++){
                if(this.array[i]==toFind){
                    return true;
                }
            }
            return false;
        }
    }
        // 查找某个元素对应的位置
        public int indexOf(int toFind) {
            if(isEmpty()){
                throw new EmptyArrayListException("该顺序表是空的");
            }else {
                for(int i = 0;i<this.UseSize;i++){
                    if(this.array[i]==toFind){
                        return i;
                    }
                }
                return -1;
            }
    }
        // 获取 pos 位置的元素
        public int get(int pos) {
        if(isEmpty()){
            throw new EmptyArrayListException("该顺序表是空的");
        }else if(PosOutofBrond(pos)){
            throw new PosOutofBrond("pos位置不合法");
        }else{

            return this.array[pos];
        }
    }
        // 给 pos 位置的元素设为 value
        public void set(int pos, int value) {
            if(isEmpty()){
                throw new EmptyArrayListException("该顺序表是空的");
            }else if(PosOutofBrond(pos)){
                throw new PosOutofBrond("pos位置不合法");
            }else{

                this.array[pos] = value;
            }
        }
        //删除第一次出现的关键字key
        public void remove(int toRemove) {
        if(isEmpty()){
            throw new EmptyArrayListException("该顺序表为空");
        }else{
            int index = -1;
            for(int i = 0;i<this.UseSize;i++){
                if(this.array[i]==toRemove){
                    index = i;
                }

            }
            if(index>=0){
                for(int i = index;i<this.UseSize-1;i++){
                    this.array[i] = array[i+1];
                }
                this.UseSize--;
            }
            else{
                System.out.println("该数找不到！");
            }
        }

        }
        // 获取顺序表长度
        public int size() {
        //判断是否为空
            return this.UseSize;
    }
        // 清空顺序表
        public void clear() {
        this.array = null;
        this.UseSize = 0;
        }

}
